package com.hmdp.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.hmdp.dto.Result;
import com.hmdp.entity.Follow;
import com.hmdp.service.IFollowService;
import com.hmdp.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
//hello
@RestController
@RequestMapping("/follow")
public class FollowController {
    @Resource
    private IFollowService iFollowService;
    @GetMapping("/or/not/{id}")
    public Result isFollowed(@PathVariable("id") Long id){
        //判断是否关注
        Long userId = UserHolder.getUser().getId();
        Integer count = iFollowService.query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count>0);

    }

    @PutMapping("/{id}/{isLike}")
    public Result isLiked(@PathVariable("id") Long id,@PathVariable("isLike") Boolean isLike) {
        Long userId = UserHolder.getUser().getId();
        if (isLike){
            //已关注
            boolean issuccess = iFollowService.remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", id));
        }
        else{
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            boolean saved = iFollowService.save(follow);
        }
        return Result.ok();
    }

}
